#!/bin/bash
##一键配置源
#开局删除默认源
yuanzhuang(){
        if [[ ! -f "/usr/bin/curl" ]];
                $1 install -y curl
        fi
        if [[ ! -f "/usr/bin/sudo" ]];then
                $1 install -y sudo
        fi
}
rm -rf /etc/apt/sources.list.d/*
rm -rf /etc/apt/sources.list
rm -rf /etc/yum.repos.d/*
##获取OS版本
sys_info(){
        osBit=`getconf LONG_BIT`
        if [ "${osBit}" != '64' ];then
                Red_Error "抱歉,暂时不支持32位系统"
                exit 32
        fi
        os_ver=`awk -F= '/^NAME/{print $2}' /etc/os-release | sed 's/"//g'| awk '{print $1}'`
        if [[ "$os_ver" == "CentOS" || "$os_ver" == "Centos" ]];then
                s=`sudo awk '{print $4}' /etc/redhat-release | cut -b 1`
                echo "the system is centos${s}"
		xt="Centos${s}"
        elif [[ "$os_ver" == "Ubuntu" ]];then
                s=`cat /etc/issue | awk '{print $2}' | cut -b 1,2`
                echo "the system is Ubuntu${s}"
		echo "os_s='ubuntu'" >> THEISPI
		xt="Ubuntu${s}"
                PI="2"
	elif [[ "$os_ver" == "Debian" ]];then
		s=`cat /etc/issue | awk '{print $3}'`
		echo "the system is Debian${s}"
		xt="Debian${s}"
        fi
        #########################################################################################
        #友情提示                                                                               #
        echo "the is source.sh"                                                                 #
        #判断包管理程序                                                                         #
        if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d/" ];then                             #
                export PI='1'                                                                   #
                PI='1'
                yuanzhuang yum                                                                       #
        fi                                                                                      #
        if [ -f "/usr/bin/apt-get" ] && [ -d "/usr/bin/dpkg" ];then                             #
                export PI='2'                                                                   #
                PI='2'
                yuanzhuang "apt-get"                                                                       #
        fi                                                                                      #
        #########################################################################################
}
sys_info
source THEISPI
#################################选择镜像源#########################
system_sources(){
	echo -e "请选择系统镜像源\n[1]阿里源\n[2]网易163源\n[3]清华大学源\n[4]搜狐镜像源\n[5]腾讯镜像站\n默认使用阿里镜像源"
	read seles
	export seles="$seles"
	if [[ -z "$seles" ]];then
		echo '使用阿里镜像源'
		Sources_URL="https://mirrors.aliyun.com";Sources_URLs="阿里镜像源"
	else
		case "$seles" in
			"1") echo '使用阿里镜像源';Sources_URL="https://mirrors.aliyun.com";Sources_URLs="阿里镜像源"
			;;
			"2") echo '使用网易163源';Sources_URL="http://mirrors.163.com";Sources_URLs="网易163源"
			;;
			"3") echo '使用清华大学源';Sources_URL="https://mirror.tuna.tsinghua.edu.cn";Sources_URLs="清华大学源"
			;;
			"4") echo  '使用华为镜像源';Sources_URL="https://mirrors.huaweicloud.com";Sources_URLs="华为镜像源"
			;;
			"5") echo '使用腾讯镜像站';Sources_URL="https://mirrors.cloud.tencent.com";Sources_URLs="腾讯镜像站"
		esac
		
	fi
}
system_sources
DR_URL_Sources(){
	echo -e "请选择Dokcer-CE源\n[1]阿里镜像源\n[2]清华大学源\n[3]腾讯源\n[4]华为镜像源\n默认使用阿里镜像源"
	read DR
	 if [[ -z "$DR" ]];then
                echo '使用默认镜像源'
                DR_URL="https://mirrors.aliyun.com";DR_URLS="阿里镜像源"
	 else
                case "$DR" in
                        "1") echo '使用阿里镜像源';DR_URL="https://mirrors.aliyun.com";DR_URLS="阿里镜像源"
                        ;;
                        "2") echo '使用清华大学源';DR_URL="https://mirror.tuna.tsinghua.edu.cn";DR_URLS="清华大学源"
                        ;;
                        "3") echo '使用腾讯镜像站';DR_URL="https://mirrors.cloud.tencent.com";DR_URLS="腾讯镜像站"
						;;
						"4") echo '使用华为镜像源';DR_URL="https://mirrors.huaweicloud.com";DR_URLS="华为镜像源"
						;;
				esac
	fi
}
DR_URL_Sources
echo -e "源配置信息如下:
当前系统版本:	${xt}
系统源:		${Sources_URLs}
Docker源:	${DR_URLS}
K8s源:		强制使用阿里源\n按回车继续\n终止请按Ctrl+C"
read Y
#######################定义乌班图源#########################################################
ubuntu_14(){
	echo -e "deb ${Sources_URL}/ubuntu/ trusty main restricted universe multiverse
deb-src ${Sources_URL}/ubuntu/ trusty main restricted universe multiverse\n
deb ${Sources_URL}/ubuntu/ trusty-security main restricted universe multiverse
deb-src ${Sources_URL}/ubuntu/ trusty-security main restricted universe multiverse\n
deb ${Sources_URL}/ubuntu/ trusty-updates main restricted universe multiverse
deb-src ${Sources_URL}/ubuntu/ trusty-updates main restricted universe multiverse\n
deb ${Sources_URL}/ubuntu/ trusty-backports main restricted universe multiverse
deb-src ${Sources_URL}/ubuntu/ trusty-backports main restricted universe multiverse" > /etc/apt/sources.list
}
ubuntu_16(){
	echo -e "deb ${Sources_URL}/ubuntu/ xenial main
deb-src ${Sources_URL}/ubuntu/ xenial main\n
deb ${Sources_URL}/ubuntu/ xenial-updates main
deb-src ${Sources_URL}/ubuntu/ xenial-updates main\n
deb ${Sources_URL}/ubuntu/ xenial universe
deb-src ${Sources_URL}/ubuntu/ xenial universe\n
deb ${Sources_URL}/ubuntu/ xenial-updates universe
deb-src ${Sources_URL}/ubuntu/ xenial-updates universe\n
deb ${Sources_URL}/ubuntu/ xenial-security main
deb-src ${Sources_URL}/ubuntu/ xenial-security main\n
deb ${Sources_URL}/ubuntu/ xenial-security universe
deb-src ${Sources_URL}/ubuntu/ xenial-security universe
" > /etc/apt/sources.list
}
ubuntu_18(){
echo -e "deb ${Sources_URL}/ubuntu/ bionic main restricted universe multiverse
deb-src ${Sources_URL}/ubuntu/ bionic main restricted universe multiverse
deb ${Sources_URL}/ubuntu/ bionic-security main restricted universe multiverse
deb-src ${Sources_URL}/ubuntu/ bionic-security main restricted universe multiverse
deb ${Sources_URL}/ubuntu/ bionic-updates main restricted universe multiverse
deb-src ${Sources_URL}/ubuntu/ bionic-updates main restricted universe multiverse
deb ${Sources_URL}/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src ${Sources_URL}/ubuntu/ bionic-proposed main restricted universe multiverse
deb ${Sources_URL}/ubuntu/ bionic-backports main restricted universe multiverse
deb-src ${Sources_URL}/ubuntu/ bionic-backports main restricted universe multiverse
" > /etc/apt/sources.list
}
ubuntu_19(){
echo -e "deb ${Sources_URL}/ubuntu/ eoan main restricted
deb ${Sources_URL}/ubuntu/ eoan-updates main restricted
deb ${Sources_URL}/ubuntu/ eoan universe
deb ${Sources_URL}/ubuntu/ eoan-updates universe
deb ${Sources_URL}/ubuntu/ eoan multiverse
deb ${Sources_URL}/ubuntu/ eoan-updates multiverse
deb ${Sources_URL}/ubuntu/ eoan-backports main restricted universe multiverse
deb ${Sources_URL}/ubuntu eoan-security main restricted
deb ${Sources_URL}/ubuntu eoan-security universe
deb ${Sources_URL}/ubuntu eoan-security multiverse
" > /etc/apt/sources.list
}
ubuntu_20(){
	echo 'deb https://mirrors.aliyun.com/ubuntu focal main restricted
deb https://mirrors.aliyun.com/ubuntu focal-updates main restricted
deb https://mirrors.aliyun.com/ubuntu focal universe
deb https://mirrors.aliyun.com/ubuntu focal-updates universe
deb https://mirrors.aliyun.com/ubuntu focal multiverse
deb https://mirrors.aliyun.com/ubuntu focal-updates multiverse
deb https://mirrors.aliyun.com/ubuntu focal-backports main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu focal-security main restricted
deb https://mirrors.aliyun.com/ubuntu focal-security universe
deb https://mirrors.aliyun.com/ubuntu focal-security multiverse
' > /etc/apt/source.list
}
############################################################################################################################
cheks(){
	echo -e "是否更新内核[y/n]\n默认N"
	read updates
	if [[ "$updates" == "y" || "$updates" == "Y" ]];then
		panduan(){
			if [ "$?" == "0" ];then
	                        echo "内核更新完成"
                	else
                        	echo "内核更新失败，请检查网络或者源配置文件"
                	fi
		}
		if [[ "$PI" == "1" ]];then
			yum update -y
			panduan
		elif [[ "$PI" == "2" ]];then
			apt-get update -y
			panduan
		fi
		echo -e "system_update=Y" >  ./k8s_install/installer_history.conf
	else
		echo '跳过更新'
	fi
}
ubuntu_surce(){
	if [[ -f "/etc/apt/sources.list.bak" ]];then
		echo '源配置已备份'
	else
		cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak
	fi
	rm -f /etc/apt/sources.list
	if [[ "${s}" == "14" ]];then
		ubuntu_14
	elif [[ "${s}" == "16" ]];then
		ubuntu_16
	elif [[ "${s}" == "18" ]];then
		ubuntu_18
	elif [[ "${s}" == "19" ]];then
		ubuntu_19
	elif [[ "${s}" == "20" ]];then
		ubuntu_20
	else
		ubuntu_20
	fi
}
centos_yum(){
	if [[ ! -d "/yum_bak/" ]];then
		sudo mkdir -p /yum_bak/
		sudo mv /etc/yum.repos.d/* /yum_bak/
	fi
	echo '是否重置YUM配置[y/n];默认y'
	read reyum
	if [[ "$reyum" == "n" ]];then
		echo '使用增加YUM方案'
	else
		rm -f /etc/yum.repos.d/*
		echo '已删除'
	fi
	echo "sudo cp -rf ./k8s_install/config/Centos-${s}.repo /etc/yum.repos.d/"
	URL1=`echo $Sources_URL | sed -e 's#https://##' -e 's#http://##'`
	sudo cp -rf ./k8s_install/config/Centos-${s}.repo /etc/yum.repos.d/
	sudo sed -i "s#URL#$URL1#g" /etc/yum.repos.d/Centos-${s}.repo 
	yum install -y sudo curl
	sudo cp -rf ./k8s_install/config/Docker-ce.repo /etc/yum.repos.d/
	sudo sed -i "s#DR_URL#$DR_URL#g" /etc/yum.repos.d/Docker-ce.repo
	if [ "$?" == "0" ];then
		echo 'Dcoker源配置完成'
	else
		echo -e "Docker源配置出错,请检查网络或执行下面的命令安装相关工具\nsudo yum install curl"
	fi
	sudo echo -e "[kubernetes]\nname=Kubernetes-Aliyun\nbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/\nenabled=1\ngpgcheck=1\nrepo_gpgcheck=1\ngpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg" > /etc/yum.repos.d/kubernetes-Aliyun.repo
	echo -e "wget -O /etc/yum.repos.d/ius-tsinghua-edu.repo https://mirrors.tuna.tsinghua.edu.cn/ius/ius-${centos_ver}.repo"
}
Debian_source(){
	mkdir -p /etc/apt/sources.list.d/
	echo -e "# deb-src [arch=amd64] ${Sources_URL}/docker-ce/linux/debian buster stable\ndeb [arch=amd64] ${Sources_URL}/docker-ce/linux/debian buster stable\n# deb-src [arch=amd64] ${Sources_URL}/docker-ce/linux/debian buster stable" > /etc/apt/sources.list.d/docker.list
	echo "deb ${Sources_URL}/debian  stable main contrib non-free" > /etc/apt/sources.list
	echo "deb-src ${Sources_URL}/debian  stable main contrib non-free" >> /etc/apt/sources.list
    echo "deb ${Sources_URL}/debian  stable-updates main contrib non-free" >> /etc/apt/sources.list
	echo "deb-src ${Sources_URL}/debian  stable-updates main contrib non-free" >> /etc/apt/sources.list
    apt update
	apt-get install apt-transport-https ca-certificates curl gnupg2 lsb-release software-properties-common -y
	apt-get install -y curl 
	apt-get install -y gnupg2 gnupg
	rm -f gpg
	curl ${Sources_URL}/docker-ce/linux/debian/gpg | apt-key add -
	rm -f gpg
	curl ${Sources_URL}/kubernetes/apt/doc/apt-key.gpg | apt-key add -
	curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
	rm -rf /var/lib/dpkg/info
	mkdir -p /var/lib/dpkg/info
	apt update
}
if [ "$PI" == "1" ];then
	centos_yum
	sudo yum clean all
	sudo yum makecache -y
	sudo yum repolist
	cheks
elif [ "$PI" == "2" ];then
	apt install -y gnupg2 gnupg
	apt install -y apt-transport-https ca-certificates curl software-properties-common
	echo -e "deb  https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
	apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6A030B21BA07F4FB
	apt update
	if [[  "$os_ver" == "Ubuntu" ]];then
		sudo echo -e "deb [arch=amd64] ${DR_URL}/docker-ce/linux/ubuntu xenial stable\n# deb-src [arch=amd64] ${DR_URL}/docker-ce/linux/ubuntu xenial stable\n# deb-src [arch=amd64] ${DR_URL}/docker-ce/linux/ubuntu xenial stable" >  /etc/apt/sources.list.d/docker-ce.list
		ubuntu_surce
	elif [[ "$os_ver" == "Debian" ]];then
		Debian_source
	fi
	cheks
fi
echo "source config completed" > ./k8s_install/so
cat ./k8s_install/so
